import org.codert.jpa.JpaBootApplication;
import org.codert.jpa.domain.order.Order;
import org.codert.jpa.domain.order.OrderItem;
import org.codert.jpa.domain.order.OrderRepository;
import org.codert.jpa.domain.person.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Optional;

/**
 * @author: TanJingyu
 * @create: 2021-08-19 17:01
 **/
@SpringBootTest(classes = JpaBootApplication.class)
public class MainSpringBootTestApplication {
    @Autowired
    private OrderRepository orderRepository;

    @Test
    public void orderInsert() {
        Order order = new Order().setAmount(new BigDecimal("34343434.54"));
        OrderItem orderItem = new OrderItem().setGoodsName("小米10");
        User user = new User().setAge(20).setUsername("李白");
        order.setItems(Collections.singletonList(orderItem));
        order.setUser(user);
        orderRepository.save(order);
    }

    @Test
    public void orderDelete() {
        orderRepository.deleteAll();
    }

    @Test
    public void orderUpdate() {
        Optional<Order> byId = orderRepository.findById(2L);
        byId.ifPresent(order -> {
            order.setAmount(new BigDecimal("12345678901234567.4367"));
            orderRepository.save(order);
        });
    }
}
